import requests
import time
from multiprocessing import Process, Pool
from threading import Thread
# import _thread

def request_zhihu(index):
     url = "https://www.zhihu.com/hot/s"
     request_body = ""
     headers = {
         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
     }
     response = requests.get(f"{url}?{request_body}", headers=headers)
     time.sleep(1)
     return response.status_code

if __name__ == "__main__":
#实现多线程
    start_time = time.time()
    t_array = []
    for index in range(10):
        t = Thread(target=request_zhihu, args=(index, ))
        t.start()
        t_array.append(t)

    [t.join() for t in t_array]
    end_time = time.time()
    print(f"线程执行时间：{end_time - start_time}s")

#实现多进程
    start_time = time.time()
    t_array = []
    for index in range(10):
        t = Process(target=request_zhihu, args=(index,))
        t.start()
        t_array.append(t)

    [t.join() for t in t_array]
    end_time = time.time()
    print(f"进程执行时间：{end_time - start_time}s")

